package cn.wyj.basic.service.impl;

import cn.wyj.basic.config.BussinessException;
import cn.wyj.basic.constant.BaseContant;
import cn.wyj.basic.deo.PhoneSmsCodeDto;
import cn.wyj.basic.service.IRegisterService;
import cn.wyj.basic.util.MD5Utils;
import cn.wyj.basic.util.StrUtils;
import cn.wyj.org.domain.PhoneRegisterDto;
import cn.wyj.user.domain.Logininfo;
import cn.wyj.user.domain.User;
import cn.wyj.user.mapper.LogininfoMapper;
import cn.wyj.user.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class IRegisterServiceImpl implements IRegisterService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void phoneRegister(PhoneRegisterDto dto) {
        this.verifyParam(dto);
        // 插， 先插Loginfo 再插user
        User user = dtoConvertUser(dto);

        Logininfo logininfo = userConvertLogininfo(user);
        // 添加
        logininfoMapper.add(logininfo);
        user.setLogininfoId(logininfo.getId());

        userMapper.add(user);

    }

    private Logininfo userConvertLogininfo(User user){
        // 把user的值赋值给Loginfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        return logininfo;
    }


    private User dtoConvertUser(PhoneRegisterDto dto){
        // 盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 密码
        String password = salt + dto.getPassword();
        String encryPwd = MD5Utils.encrypByMd5(password);


        return User
                .builder()
                .username(dto.getPhone())
                .phone(dto.getPhone())
                .salt(salt)
                .password(encryPwd)
                .state(1)
                .createtime(new Date())
                .build();


    }




    private void verifyParam(PhoneRegisterDto dto) {
        // 判断非空

        // 两次密码是否一致
        if (!dto.getPassword().equals(dto.getPasswordRepeat())) {
            throw new BussinessException("两次密码不一致");
        }

        // 判断验证码是否正确 / 两次手机号是否一致
        String redisSmsCode = (String) redisTemplate.opsForValue().get(String.format(BaseContant.VerfityCodeCons.REGISTER_SMSCODE_PHONE,
                dto.getPhone()));
        if (StringUtils.isBlank(redisSmsCode)) {
            throw new BussinessException("验证码错误!");
        }
        String[] smsCode = redisSmsCode.split(":");
        if (!smsCode[0].equals(dto.getSmsCode())) {
            throw new BussinessException("验证码错误!");
        }
        // 是否注册过
        User byPhone = userMapper.findByPhone(dto.getPhone());
        if (null != byPhone) {
            throw new BussinessException("gun");
        }
    }
}
